/*
 * Te_Def_View.java
 *
 * Created on 2009年2月24日, 下午8:54
 */

package register.view;

import java.beans.XMLEncoder;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import register.model.Te_Info_Model;

/**
 *
 * @author  马凯
 */

public class Te_Def_View extends javax.swing.JFrame {
    private String tpCourse;
    private String stName;
    private String stID;
    private String stCID;
    private ResultSet student;
    private ResultSet course;
    private Statement statement;
    private Connection con;
    private String sqlQuery;
    private String[] temp=new String[10];
    private int i=0;
    /** Creates new form Te_Def_View */
    public Te_Def_View() {
        initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        cid_TF = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        psw_TF = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Card ID:");

        jLabel2.setText("Password:");

        jButton1.setText("Ok");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jMenu1.setText("File");
        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edit");
        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 69, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(psw_TF, javax.swing.GroupLayout.DEFAULT_SIZE, 145, Short.MAX_VALUE)
                            .addComponent(cid_TF, javax.swing.GroupLayout.DEFAULT_SIZE, 145, Short.MAX_VALUE)))
                    .addComponent(jButton1, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(cid_TF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(psw_TF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        Te_Info_Model tim = new Te_Info_Model();
        tim.setName(this.cid_TF.getText());
        tim.setPsw(this.psw_TF.getText());
        System.out.println("The card ID is "+this.cid_TF.getText());
        System.out.println("The password is "+this.psw_TF.getText());
        try{
            XMLEncoder encoder =
                new XMLEncoder(
                new BufferedOutputStream(new FileOutputStream(this.cid_TF.getText()+".xml")));
            System.out.println("The file path is "+this.cid_TF.getText()+".xml");
            encoder.writeObject(tim);
            encoder.close();
        }catch(Exception e){
            e.printStackTrace();
        }
         try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/isc?user=root&password=root");
            con.setCatalog("isc");
            statement = con.createStatement();

//            sqlQuery = "SELECT course_id FROM isc.teacher, isc.course WHERE teacher_card="+ this.cid_TF.getText();
            sqlQuery="SELECT registeredcourse.course_id, registeredcourse.student_id, registeredcourse.student_name, " +
                    "registeredcourse.student_card FROM isc.teacher, isc.registeredcourse " +
                    "WHERE teacher_card='" + this.cid_TF.getText() +"'";
            course = statement.executeQuery(sqlQuery);

            while(course.next()){
                 tpCourse=course.getString("course_id");
                    stName=course.getString("student_name");
                    stID=course.getString("student_id");
                    stCID=course.getString("student_card");
                    System.out.println("Course: "+tpCourse);
                    System.out.println("Name: " + stName);
                    System.out.println("ID: " + stID);
                    System.out.println("CID: " + stCID);
//                 temp[i]=tpCourse;
//                 i++;
//                 System.out.println(i+" row found");
                  //Start to get Student's name, cid and id
//                 this.getStudent();
//                 student.close();
            }

//            course.close();
            //statement.close();
            //con.close();
        }catch(Exception e){e.printStackTrace();}
    }//GEN-LAST:event_jButton1ActionPerformed

    public void getStudent()
    {
        try{
                for(int j=0;j<temp.length;j++)
                {
                 
                 sqlQuery="SELECT student_id, student_card, student_name FROM registeredcourse " +
                         "WHERE course_id='" + temp[j] +"'";
                 student=statement.executeQuery(sqlQuery);

                 student.beforeFirst();
                 while(student.next())
                 {
                    stName=student.getString("student_name");
                    stID=student.getString("student_id");
                    stCID=student.getString("student_card");

                    System.out.println("Name: " + stName);
                    System.out.println("ID: " + stID);
                    System.out.println("CID: " + stCID);


                 }
                 student.beforeFirst();
                }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

        }


    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Te_Def_View().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField cid_TF;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JTextField psw_TF;
    // End of variables declaration//GEN-END:variables
    
}
